﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

using E.Web.UI.Page;
using E.BLL;
using E.Model;
using E.Enum;

using X.Common;

namespace E.Web.UI
{
    public class AjaxPage : BasePage
    { 
        public AjaxPage()
        {
            string type = XRequest.GetString("t");

            switch (type)
            {
                case "getExamInfo":
                    GetExamInfo(XRequest.GetInt("examid", -1), XRequest.GetInt("cid", -1));
                    break;
                case "getExamSelfInfo":
                    GetExamSelfInfo(XRequest.GetInt("examid", -1), XRequest.GetInt("cid", -1));
                    break;
                case "setTestInfo":
                    SetTestInfo(XRequest.GetString("answers"), XRequest.GetString("subjectivequestionAnswer"), XRequest.GetInt("score", 0), XRequest.GetInt("examid", -1), XRequest.GetInt("cid", -1));
                    break;
                case "getStudyLog":
                    GetStudyLog(XRequest.GetInt("id", -1));
                    break;
                case "updateStudyLog":
                    UpdateStudyLog(XRequest.GetInt("id", -1), XRequest.GetString("chapters"), XRequest.GetInt("progress", -1));
                    break;
                case "getSurveyInfo":
                    GetSurveyInfo(XRequest.GetInt("id", -1), XRequest.GetInt("cid", -1));
                    break;
                case "getCertificateSurveyReport":
                    GetCertificateSurveyReport(XRequest.GetInt("id", -1));
                    break;
                case "addCourseSurveyLog":
                    AddCourseSurveyLog(XRequest.GetInt("id", -1), XRequest.GetString("content"), XRequest.GetString("comment"));
                    break;
                case "addCertificateSurveyLog":
                    AddCertificateSurveyLog(XRequest.GetInt("id", -1), XRequest.GetString("content"), XRequest.GetString("comment"));
                    break;
                case "getCourseWareList":
                    GetCourseWareList(XRequest.GetInt("cid", -1));
                    break;
                case "setBookMark":
                    SetBookMark(XRequest.GetInt("courseid", -1), XRequest.GetInt("pagenumber", -1));
                    break;
                case "setInternalStudyProcess":
                    SetInternalStudyProcess(XRequest.GetInt("courseid", -1), XRequest.GetInt("projectid", -1), XRequest.GetInt("process", -1));
                    break;
                case "getPublicCourseWareList":
                    GetPublicCourseWareList(XRequest.GetInt("cid", -1));
                    break;
                case "setInternaPubliclStudyProcess":
                    SetInternalPublicStudyProcess(XRequest.GetInt("courseid", -1), XRequest.GetInt("projectid", -1), XRequest.GetInt("process", -1), XRequest.GetString("chapters"));
                    break;
                case "getPublicStudyLog":
                    GetPublicStudyLog(XRequest.GetInt("cid", -1));
                    break;
                case "t":
                    T();
                    break;
                case "isNeedFinishPrevLevelCourse":
                    isNeedFinishPrevLevelCourse(XRequest.GetInt("courseId", -1),userid);
                    break;
                default: break;
            }
        }

        private void isNeedFinishPrevLevelCourse(int courseId, int studentId)
        {
            bool result = (new CourseBLL()).IsNeedFinishPrevCourse(courseId, userid);
            StringBuilder sb = new StringBuilder();
            sb.Append(result.ToString());
            ResponseXML(sb);

        }

        private void GetExamSelfInfo(int examid, int categoryid)
        {
            if (examid != -1)
            {
                ExamInfoModel examModel = (new ExamBLL()).GetExamInfoByID(examid);

                if (examModel.ID != -1)
                {
                    QuestionBLL bll = new QuestionBLL();
                    IList<int> allList = new List<int>();

                    IList<int> tmplist = bll.GetQuestionIDByCourse(areaid, (new ExamBLL()).GetCourseIDByExamID(examid));

                    int count = tmplist.Count >= 5 ? 5 : tmplist.Count;

                    IList<int> idList = GetRandomValue(count, tmplist);

                    foreach (int id in idList)
                    {
                        allList.Add(id);
                    }
                
                    DateTime now = DateTime.Now;

                    TestModel testModel = new TestModel();
                    testModel.UserID = userid;
                    testModel.ExamID = examid;
                    testModel.QuestionIDArray = allList;
                    testModel.CreatedTime = now;
                    testModel.AreaID = areaid;

                    IList<QuestionModel> dataList = bll.GetQuestionDetailInfoListSelfByParms(testModel);

                    if (dataList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<Data examtype=\"" + examModel.CourseCertificateName + "\" t=\"" + examModel.ExamTime + "\" st=\"" + (new ExamBLL()).GetUserTestStartTime(examid, userid).StartTime + "\" ct=\"" + now + "\" >");
                        foreach (QuestionModel model in dataList)
                        {
                            sb.Append("<Info>");
                            sb.Append("<ID>" + model.ID + "</ID>");
                            sb.Append("<Title><![CDATA[" + model.Title + "]]></Title>");
                            sb.Append("<TypeID>" + model.CategoryID + "</TypeID>");
                            sb.Append("<Type>" + model.CategoryName + "</Type>");
                            sb.Append("<CourseName>" + model.CourseName + "</CourseName>");
                            sb.Append("<Score>" + model.Score + "</Score>");
                            sb.Append("<Description><![CDATA[" + model.Description + "]]></Description>");
                            sb.Append("<KnowledgePoint><![CDATA[" + model.KnowledgePoint + "]]></KnowledgePoint>");
                            sb.Append("<ImgUrl>" + model.ImgUrl + "</ImgUrl>");
                            sb.Append("<SoundUrl>" + model.SoundUrl + "</SoundUrl>");
                            sb.Append("<Options>");

                            foreach (QuestionOptionModel optionModel in model.OptionList)
                            {
                                sb.Append("<OptionInfo>");
                                sb.Append("<ID>" + optionModel.ID + "</ID>");
                                sb.Append("<SerialNumber>" + optionModel.SerialNumber + "</SerialNumber>");
                                sb.Append("<Title><![CDATA[" + optionModel.OptionTitle + "]]></Title>");
                                sb.Append("<IsCorrect>" + (optionModel.IsCorrect ? "1" : "0") + "</IsCorrect>");
                                sb.Append("</OptionInfo>");
                            }

                            sb.Append("</Options>");
                            sb.Append("</Info>");
                        }
                        sb.Append("</Data>");
                        ResponseXML(sb);
                    }

                }
            }
        }

        private void GetExamInfo(int examid, int categoryid)
        {
            if (examid != -1)
            {
                ExamInfoModel examModel = (new ExamBLL()).GetExamInfoByID(examid);

                if (examModel.ID != -1)
                {
                    QuestionBLL bll = new QuestionBLL();
                    IList<int> allList = new List<int>();
                    if (examModel.IsRandom == 0)
                    {
                        string questionStr = examModel.QuestionIDArray;
                        string[] questionArr = questionStr.Split(',');
                        if (questionStr.Length > 0)
                        {
                            allList = (new ExamBLL()).GetExamCustomQuestionArray(questionArr);
                        }
                    }
                    //Dictionary<int, int> examSetting = bll.GetExamSetting(examid);
                    IList<ShortExamSettingModel> examSetting = (new ExamBLL()).GetExamShortSetting(examid);

                    //userid = 1087;
                    //areaid = 1;


                    if (examSetting.Count > 0)
                    {
                        foreach (ShortExamSettingModel settingModel in examSetting)
                        {
                            IList<int> tmplist = bll.GetQuestionIDByCategory(settingModel.QuestionCategoryID, areaid, settingModel.CourseID, 0);
                            IList<int> tmplist_re = bll.GetQuestionIDByCategory(settingModel.QuestionCategoryID, areaid, settingModel.CourseID, 1);
                            IList<int> idList = GetRandomValue(settingModel.QuestionQuantity, tmplist);
                            IList<int> idList_re = GetRandomValue(settingModel.RequiredQuestionQuantity, tmplist_re);
                            foreach (int id in idList)
                            {
                                allList.Add(id);
                            }
                            foreach (int id in idList_re)
                            {
                                allList.Add(id);
                            }
                        }
                    }
                    DateTime now = DateTime.Now;

                    TestModel testModel = new TestModel();
                    testModel.UserID = userid;
                    testModel.ExamID = examid;
                    testModel.QuestionIDArray = allList;
                    testModel.CreatedTime = now;
                    testModel.AreaID = areaid;

                    //IList<QuestionModel> dataList = bll.GetQuestionDetailInfoList(allList);
                    IList<QuestionModel> dataList = bll.GetQuestionDetailInfoListByParms(testModel);

                    if (dataList.Count > 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<Data examtype=\"" + examModel.CourseCertificateName + "\" t=\"" + examModel.ExamTime + "\" st=\"" + (new ExamBLL()).GetUserTestStartTime(examid, userid).StartTime + "\" ct=\"" + now + "\" >");
                        foreach (QuestionModel model in dataList)
                        {
                            sb.Append("<Info>");
                            sb.Append("<ID>" + model.ID + "</ID>");
                            sb.Append("<Title><![CDATA[" + model.Title + "]]></Title>");
                            sb.Append("<TypeID>" + model.CategoryID + "</TypeID>");
                            sb.Append("<Type>" + model.CategoryName + "</Type>");
                            sb.Append("<CourseName>" + model.CourseName + "</CourseName>");
                            sb.Append("<Score>" + model.Score + "</Score>");
                            sb.Append("<Description><![CDATA[" + model.Description + "]]></Description>");
                            sb.Append("<KnowledgePoint><![CDATA[" + model.KnowledgePoint + "]]></KnowledgePoint>");
                            sb.Append("<ImgUrl>" + model.ImgUrl + "</ImgUrl>");
                            sb.Append("<SoundUrl>" + model.SoundUrl + "</SoundUrl>");
                            sb.Append("<Options>");

                            foreach (QuestionOptionModel optionModel in model.OptionList)
                            {
                                sb.Append("<OptionInfo>");
                                sb.Append("<ID>" + optionModel.ID + "</ID>");
                                sb.Append("<SerialNumber>" + optionModel.SerialNumber + "</SerialNumber>");
                                sb.Append("<Title><![CDATA[" + optionModel.OptionTitle + "]]></Title>");
                                sb.Append("<IsCorrect>" + (optionModel.IsCorrect ? "1" : "0") + "</IsCorrect>");
                                sb.Append("</OptionInfo>");
                            }

                            sb.Append("</Options>");
                            sb.Append("</Info>");
                        }
                        sb.Append("</Data>");
                        ResponseXML(sb);
                    }

                }
            }
        }

        private void SetTestInfo(string answerArr,string subjectivequestionAnswer, int score, int examid, int categoryid)
        {
            TestModel model = new TestModel();


            //userid = 1087;
            //areaid = 1;

            model.UserID = userid;
            model.ExamID = examid;
            //model.ExamID = 1;
            model.AnswerArray = answerArr;
            model.SubjectiveQuestionAnswer = subjectivequestionAnswer;
            model.SubmitTime = DateTime.Now;
            model.Score = score;
            int rst = (new ExamBLL()).UpdateTestInfo(model);
            ResponseSimpleXML(rst.ToString());
        }

        private void GetStudyLog(int id)
        {
            StringBuilder xmlnode = new StringBuilder();
            xmlnode.Append("<manifest>");
            xmlnode.Append("	<metadata>");
            xmlnode.Append("		<schema>ADL SCORM</schema>");
            xmlnode.Append("		<schemaversion>CAM 1.3</schemaversion>");
            xmlnode.Append("		<location>");
            if (id != -1)
            {
                StudyLogModel model = (new StudyBLL()).GetStudyShortLogByID(id);
                if (model.ID != -1)
                {
                    xmlnode.Append(model.CourseID + "$$" + model.Chapters);
                }
            }
            xmlnode.Append("</location>");
            xmlnode.Append("	</metadata>");
            xmlnode.Append("</manifest>");
            ResponseXML(xmlnode);
        }

        private void UpdateStudyLog(int id, string chapters, int progress)
        {
            StudyLogModel model = new StudyLogModel();
            model.ID = id;
            model.Chapters = chapters;
            model.Progress = progress;
            int rst = (new StudyBLL()).UpdateStudyLog(model);
            if (rst > 0)
            {
                ResponseSimpleXML("success");
            }
            else
            {
                ResponseSimpleXML("failed");
            }
        }

        private void GetSurveyInfo(int examid, int categoryid)
        {
            //int examID = Utils.StrToInt(Utils.GetCookie("examid"), -1);
            //int cid = Utils.StrToInt(Utils.GetCookie("cid"), -1);
            
            //int cid = 1;
            //int cid = 2;
            int t = Utils.StrToInt(Utils.GetCookie("t"), -1);
            t = 1;
            //userid = 1090;

            SurveyBLL bll = new SurveyBLL();
            StringBuilder sb;

            if (examid != -1 && categoryid != -1)
            {
                sb = new StringBuilder();
                switch ((EnumExamCategory)categoryid)
                {
                    case EnumExamCategory.CourseExam:
                        CourseSurveyModel courseModel = new CourseSurveyModel();

                        switch (t)
                        {
                            case -1:
                                courseModel = bll.GetCourseSurveyInfo(examid, userid);
                                break;
                            case 1:
                                courseModel = bll.GetCourseSurveyInfo_Internal(examid, userid);
                                break;
                            case 2:
                                courseModel = bll.GetCourseSurveyInfo_Internal_P(examid, userid);
                                break;
                            default:
                                break;
                        }
                        if (courseModel.ID != -1)
                        {
                            sb.Append("<Data>\r\n");
                            //sb.Append("  <ID>" + courseModel.ID + "</ID>\r\n");
                            //sb.Append("  <CID>" + courseModel.CourseID + "</CID>\r\n");
                            //sb.Append("  <CName>" + courseModel.CourseName + "</CName>\r\n");
                            //sb.Append("  <TeacherName>" + courseModel.TeacherName + "</TeacherName>\r\n");
                            sb.Append("  <SurveyInfo ID=\"" + courseModel.ID + "\" CID=\"" + courseModel.CourseID + "\" CName=\"" + courseModel.CourseName + "\" TeacherName=\"" + courseModel.TeacherName + "\">\r\n");
                            foreach (SurveyCategoryAndOptionModel contentModel in courseModel.OptionList)
                            {
                                sb.Append("    <CategoryInfo>\r\n");
                                sb.Append("      <CategoryID>" + contentModel.ID + "</CategoryID>\r\n");
                                sb.Append("      <CategoryName>" + contentModel.CategoryName + "</CategoryName>\r\n");
                                sb.Append("      <Options>\r\n");
                                foreach (KeyValuePair<int, string> kvp in contentModel.OptionList)
                                {
                                    sb.Append("         <Option>\r\n");
                                    sb.Append("             <OptionID>" + kvp.Key + "</OptionID>\r\n");
                                    sb.Append("             <OptionName>" + kvp.Value + "</OptionName>\r\n");
                                    sb.Append("         </Option>\r\n");
                                }
                                sb.Append("      </Options>\r\n");
                                sb.Append("    </CategoryInfo>\r\n");
                            }
                            sb.Append("  </SurveyInfo>\r\n");
                            sb.Append("</Data>\r\n");
                        }
                        break;
                    case EnumExamCategory.CertificateExam:
                        CertificateSurveyModel certificateModel = new CertificateSurveyModel();

                        switch (t)
                        {
                            case -1:
                                certificateModel = bll.GetCertificateSurveyInfo(examid);
                                break;
                            case 1:
                                certificateModel = bll.GetCertificateSurveyInfo_Internal(examid);
                                break;
                            default:
                                break;
                        }
                        if (certificateModel.ID != -1)
                        {
                            sb.Append("<Data>\r\n");
                            //sb.Append("  <ID>" + certificateModel.ID + "</ID>\r\n");
                            //sb.Append("  <CertificateName>" + certificateModel.CertificateName + "</CertificateName>\r\n");
                            sb.Append("  <SurveyInfo ID=\"" + certificateModel.ID + "\" CertificateName=\"" + certificateModel.CertificateName + "\">\r\n");
                            foreach (SurveyCategoryAndOptionModel contentModel in certificateModel.OptionList)
                            {
                                sb.Append("    <CategoryInfo>\r\n");
                                sb.Append("      <CategoryID>" + contentModel.ID + "</CategoryID>\r\n");
                                sb.Append("      <CategoryName>" + contentModel.CategoryName + "</CategoryName>\r\n");
                                sb.Append("      <Options>\r\n");
                                foreach (KeyValuePair<int, string> kvp in contentModel.OptionList)
                                {
                                    sb.Append("         <Option>\r\n");
                                    sb.Append("             <OptionID>" + kvp.Key + "</OptionID>\r\n");
                                    sb.Append("             <OptionName>" + kvp.Value + "</OptionName>\r\n");
                                    sb.Append("         </Option>\r\n");
                                }
                                sb.Append("      </Options>\r\n");
                                sb.Append("    </CategoryInfo>\r\n");
                            }
                            sb.Append("  </SurveyInfo>\r\n");
                            sb.Append("</Data>\r\n");
                        }
                        break;
                    default: break;
                }
                ResponseXML(sb);
            }
        }

        private void GetCertificateSurveyReport(int certificateid)
        {
            if (certificateid != -1)
            {
                StringBuilder sb = new StringBuilder();
                SurveyBLL bll = new SurveyBLL();
                //IList<SurveyCategoryAndOptionModel> scaomList = bll.GetCertificateSurveyCategoryList(certificateid);
                CertificateSurveyReportModel reportModel = bll.GetCertificateSurveyReport(certificateid);
                if (reportModel.ID != -1)
                {
                    sb.Append("<Data>\r\n");
                    sb.Append("  <SurveyInfo>\r\n");
                    foreach (SurveyCategoryAndOptionModel contentModel in reportModel.OptionList)
                    {
                        sb.Append("    <CategoryInfo>\r\n");
                        sb.Append("      <CategoryID>" + contentModel.ID + "</CategoryID>\r\n");
                        sb.Append("      <CategoryName>" + contentModel.CategoryName + "</CategoryName>\r\n");
                        sb.Append("      <Options>\r\n");
                        foreach (KeyValuePair<int, string> kvp in contentModel.OptionList)
                        {
                            sb.Append("        <OptionID>" + kvp.Key + "</OptionID>\r\n");
                            sb.Append("        <OptionName>" + kvp.Value + "</OptionName>\r\n");
                        }
                        sb.Append("      </Options>\r\n");
                        sb.Append("    </CategoryInfo>\r\n");
                    }
                    sb.Append("    <Votes>\r\n");
                    foreach (KeyValuePair<string, string> kvp2 in reportModel.VoteList)
                    {
                        sb.Append("      <Vote>\r\n");
                        sb.Append("        <OptionVote>" + kvp2.Key + "</OptionVote>\r\n");
                        sb.Append("        <CommentVote>" + kvp2.Value + "</CommentVote>\r\n");
                        sb.Append("      </Vote>\r\n");
                    }
                    sb.Append("    </Votes>\r\n");
                    sb.Append("  </SurveyInfo>\r\n");
                    sb.Append("</Data>\r\n");
                    ResponseXML(sb);
                }
            }
        }

        private void AddCourseSurveyLog(int surveyid, string surveyContent, string comment)
        {
            SurveyLogModel model = new SurveyLogModel();
            model.SurveyID = surveyid;
            model.UserID = userid;
            model.SurveyContent = surveyContent;
            model.Comment = comment;
            model.SubmitTime = DateTime.Now;
            (new SurveyBLL()).AddCourseSurveyLog(model);

            //Utils.ClearCookie("examid");
            //Utils.ClearCookie("cid");
        }

        private void AddCertificateSurveyLog(int surveyid, string surveyContent, string comment)
        {
            SurveyLogModel model = new SurveyLogModel();
            model.SurveyID = surveyid;
            model.UserID = userid;
            model.SurveyContent = surveyContent;
            model.Comment = comment;
            model.SubmitTime = DateTime.Now;
            (new SurveyBLL()).AddCertificateSurveyLog(model);

            //Utils.ClearCookie("examid");
            //Utils.ClearCookie("cid");
        }

        #region 内训课件

        private void GetCourseWareList(int courseid)
        {
            CourseBLL bll = new CourseBLL();
            CourseModel courseModel = new CourseModel();
            courseModel = bll.GetInternalCourseInfoById(courseid);
            int process = (new StudyBLL()).GetUserInternalStudyProcess(userid, courseid);

            IList<CourseWareModel> courseWareList = bll.GetCourseWareListByCourseID(courseid);

            StringBuilder sb = new StringBuilder();
            sb.Append("<Data>\r\n");
            sb.Append("  <CourseInfo CourseName=\"" + courseModel.Name + "\" CnPName=\"" + courseModel.CnPName + "\" BookMark=\"" + bll.GetUserBookMark(userid, courseid) + "\" Process=\"" + process + "\" />\r\n");
            sb.Append("  <CourseWare>\r\n");

            if (courseWareList.Count > 0)
            {
                foreach (CourseWareModel model in courseWareList)
                {
                    sb.Append("    <Info>\r\n");
                    sb.Append("      <ID>" + model.ID + "</ID>");
                    sb.Append("      <SerialNumber>" + model.SerialNumber + "</SerialNumber>");
                    sb.Append("      <ImgUrl>" + model.ImgUrl + "</ImgUrl>");
                    sb.Append("      <SoundUrl>" + model.SoundUrl + "</SoundUrl>");
                    sb.Append("      <VideoUrl>" + model.VideoUrl + "</VideoUrl>");
                    sb.Append("      <Content><![CDATA[" + model.Content + "]]></Content>");
                    sb.Append("    </Info>\r\n");
                }
            }

            sb.Append("  </CourseWare>\r\n");
            sb.Append("</Data>");

            ResponseXML(sb);
        }

        private void SetBookMark(int courseid, int pageNumber)
        {
            (new CourseBLL()).BookMarkHandle(userid, courseid, pageNumber);
        }

        private void SetInternalStudyProcess(int courseid, int projectid, int process)
        {
            (new StudyBLL()).UpdateUserInternalProcess(userid, courseid, projectid, process);
        }

        #endregion

        #region 公共课

        private void GetPublicCourseWareList(int courseid)
        {
            CourseBLL bll = new CourseBLL();
            CourseModel courseModel = new CourseModel();
            courseModel = bll.GetInternalCourseInfoById(courseid);
            int process = (new StudyBLL()).GetUserPublicStudyProgress(userid, courseid);

            IList<CourseWareModel> courseWareList = bll.GetCourseWareListByCourseID(courseid);

            StringBuilder sb = new StringBuilder();
            sb.Append("<Data>\r\n");
            sb.Append("  <CourseInfo CourseName=\"" + courseModel.Name + "\" CnPName=\"" + courseModel.CnPName + "\" BookMark=\"1\" Process=\"" + process + "\" />\r\n");
            sb.Append("  <CourseWare>\r\n");

            if (courseWareList.Count > 0)
            {
                foreach (CourseWareModel model in courseWareList)
                {
                    sb.Append("    <Info>\r\n");
                    sb.Append("      <ID>" + model.ID + "</ID>");
                    sb.Append("      <SerialNumber>" + model.SerialNumber + "</SerialNumber>");
                    sb.Append("      <ImgUrl>" + model.ImgUrl + "</ImgUrl>");
                    sb.Append("      <SoundUrl>" + model.SoundUrl + "</SoundUrl>");
                    sb.Append("      <VideoUrl>" + model.VideoUrl + "</VideoUrl>");
                    sb.Append("      <Content><![CDATA[" + model.Content + "]]></Content>");
                    sb.Append("    </Info>\r\n");
                }
            }

            sb.Append("  </CourseWare>\r\n");
            sb.Append("</Data>");

            ResponseXML(sb);
        }

        private void SetInternalPublicStudyProcess(int courseid, int projectid, int process, string chapters)
        {
            (new StudyBLL()).UpdateUserInternalPublicProcess(userid, courseid, projectid, process, chapters);
        }

        private void GetPublicStudyLog(int id)
        {
            StringBuilder xmlnode = new StringBuilder();
            xmlnode.Append("<manifest>");
            xmlnode.Append("	<metadata>");
            xmlnode.Append("		<schema>ADL SCORM</schema>");
            xmlnode.Append("		<schemaversion>CAM 1.3</schemaversion>");
            xmlnode.Append("		<location>");
            if (id != -1)
            {
                StudyLogModel model = (new StudyBLL()).GetPublicStudyShortLogByID(id, userid);
                if (model.ID != -1)
                {
                    xmlnode.Append(model.CourseID + "$$" + model.Chapters);
                }
            }
            xmlnode.Append("</location>");
            xmlnode.Append("	</metadata>");
            xmlnode.Append("</manifest>");
            ResponseXML(xmlnode);
        }

        #endregion

        private void T()
        {
            ResponseJSON("test");
        }

        private void ResponseSimpleXML(string result)
        {
            StringBuilder xmlnode = new StringBuilder();
            xmlnode.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
            xmlnode.Append("<result>");
            xmlnode.Append(result);
            xmlnode.Append("</result>");
            ResponseXML(xmlnode);
        }

        #region Helper
        /// <summary>
        /// 向页面输出xml内容
        /// </summary>
        /// <param name="xmlnode">xml内容</param>
        private void ResponseXML(System.Text.StringBuilder xmlnode)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/XML";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Cache.SetNoStore();
            System.Web.HttpContext.Current.Response.Write(xmlnode.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 输出json内容
        /// </summary>
        /// <param name="json"></param>
        private void ResponseJSON(string json)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "application/json";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Cache.SetNoStore();
            System.Web.HttpContext.Current.Response.Write(json);
            System.Web.HttpContext.Current.Response.End();
        }

        //private void ResponseJSON<T>(T jsonobj)
        //{
        //    ResponseJSON(JavaScriptConvert.SerializeObject(jsonobj));
        //}
        #endregion
    }
}
